package com.tao.design.composite

/**
 *
 * @ProjectName:    lab
 * @Package:        com.tao.design.composite
 * @ClassName:      Composite
 * @Description:     java类作用描述
 * @Author:         wangxf
 * @CreateDate:     2022/4/27 14:02
 * @UpdateUser:     更新者
 * @UpdateDate:     2022/4/27 14:02
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */

/**
 * 通过树形方式来创建对象的结构
 *
 * 可以将对象的集合以及个别对象一视同仁
 *
 * 树结构中使用：   组合迭代器 空迭代器
 */
interface Component {
    fun add(menuItem: MenuItem);
    fun del(menuItem: MenuItem)
    fun getChild(): MutableList<MenuItem>;

    fun getMenuName(): String;
}

abstract class MenuComponent : Component {

    override fun add(menuItem: MenuItem) {
        throw UnsupportedClassVersionError();
    }

    override fun del(menuItem: MenuItem) {
        throw UnsupportedClassVersionError();
    }

    override fun getChild(): MutableList<MenuItem> {
        throw UnsupportedClassVersionError();
    }

    override fun getMenuName(): String {
        throw UnsupportedClassVersionError();
    }
}

class MenuItem(val name: String) : MenuComponent() {

    override fun getMenuName(): String {
        return name;
    }


}

class Menu : MenuComponent() {
    val menu: MutableList<MenuItem> = ArrayList<MenuItem>();
    override fun add(menuItem: MenuItem) {
        menu.add(menuItem);
    }

    override fun del(menuItem: MenuItem) {
        menu.remove(menuItem)
    }

    override fun getChild(): MutableList<MenuItem> {
        return menu
    }


    fun print(menu: MutableList<MenuItem>) {
        var iterator = menu.iterator();
        while (iterator.hasNext()) {
            var item = iterator.next();
            if (item.getChild().size > 0) {
                print(item.getChild());
            }
        }
    }


}

fun main() {
    val menuItem = MenuItem("x")
    val menu = Menu();
    menu.add(menuItem);
    menu.print(menu.getChild());
}
